package com.qch.rabbitmq.demo.controller;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

/**
 * @Author qch
 * @Date 2021/5/14
 */
@RestController
public class MessageController {

    //AmqpTemplate:快捷键Ctrl+Alt+B 查看实现
    @Autowired
    private AmqpTemplate rabbitTemplate;

    @RequestMapping("/rabbit/{message}")
    public String receive(@PathVariable String message) throws UnsupportedEncodingException {
        MessageProperties properties = MessagePropertiesBuilder.newInstance().setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
                .setContentEncoding("utf-8").
                        setHeader("hello", "qch").build();
        Message message1 = MessageBuilder.withBody(message.getBytes("utf-8")).andProperties(properties).build();
        rabbitTemplate.send("ex.boot", "key.boot", message1);
        return "ok";
    }
}
